package com.sanqing.dao;

import java.util.Date;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

import com.sanqing.po.Training;

/**
 * A data access object (DAO) providing persistence and search support for
 * Training entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.sanqing.po.Training
 * @author MyEclipse Persistence Tools
 */

public class TrainingDao extends BaseHibernateDao {
	private static final Log log = LogFactory.getLog(TrainingDao.class);
	// property constants
	public static final String NAME = "name";
	public static final String PURPOSE = "purpose";
	public static final String DAYS = "days";
	public static final String DATUM = "datum";
	public static final String TEACHER = "teacher";
	public static final String STATUS = "status";
	public static final String EFFECT = "effect";
	public static final String SUMMARIZE = "summarize";

	public void save(Training transientInstance) {
		log.debug("saving Training instance");
		try {
			Session session = getSession();
			Transaction tx = session.beginTransaction();
			session.save(transientInstance);
			tx.commit();
			closeSession();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Training persistentInstance) {
		log.debug("deleting Training instance");
		try {
			Session session = getSession();
			Transaction tx = session.beginTransaction();
			session.delete(persistentInstance);
			tx.commit();
			closeSession();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	public void update(Training persistentInstance) {
		log.debug("updating Training instance");
		try {
			Session session = getSession();
			Transaction tx = session.beginTransaction();
			session.update(persistentInstance);
			tx.commit();
			closeSession();
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}

	public Training findById(java.lang.Integer id) {
		log.debug("getting Training instance with id: " + id);
		try {
			Training instance = (Training) getSession().get(
					"com.sanqing.po.Training", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Training instance) {
		log.debug("finding Training instance by example");
		try {
			List results = getSession().createCriteria(
					"com.sanqing.po.Training").add(Example.create(instance))
					.list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Training instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Training as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByPurpose(Object purpose) {
		return findByProperty(PURPOSE, purpose);
	}

	public List findByDays(Object days) {
		return findByProperty(DAYS, days);
	}

	public List findByDatum(Object datum) {
		return findByProperty(DATUM, datum);
	}

	public List findByTeacher(Object teacher) {
		return findByProperty(TEACHER, teacher);
	}

	public List findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	public List findByEffect(Object effect) {
		return findByProperty(EFFECT, effect);
	}

	public List findBySummarize(Object summarize) {
		return findByProperty(SUMMARIZE, summarize);
	}

	public List findAll() {
		log.debug("finding all Training instances");
		try {
			String queryString = "from Training";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Training merge(Training detachedInstance) {
		log.debug("merging Training instance");
		try {
			Training result = (Training) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Training instance) {
		log.debug("attaching dirty Training instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Training instance) {
		log.debug("attaching clean Training instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}